package com.techzhi.redis.jedis;

import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisClientConfig;
import redis.clients.jedis.UnifiedJedis;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * @ClassName JedisTestMain
 * @Author shouzhi
 * @Date 2025/10/10 18:51
 * @Version 1.0
 * @Description 参考：https://redis.io/docs/latest/develop/clients/jedis/
 **/
public class JedisTestMain {

    private static final String REDIS_HOST = "192.168.16.6";
    private static final int REDIS_PORT = 6379;
    private static final String REDIS_PASSWORD = "Baize_123";

    public static void main(String[] args) throws URISyntaxException {

        // 1、URI方式必须设置密码在这里，否则上面的jedisClientConfig设置的密码不会生效
        //URI uri = new URI("redis://default:" + REDIS_PASSWORD + "@" + REDIS_HOST + ":" + REDIS_PORT);
        //UnifiedJedis jedis = new UnifiedJedis(uri);

        // 2、HostAndPort方式，这种情况JedisClientConfig设置的密码会生效
        JedisClientConfig jedisClientConfig = DefaultJedisClientConfig
                .builder()
                .user("default")
                .password(REDIS_PASSWORD)
                .database(0)
                .build();
        HostAndPort hostAndPort = new HostAndPort(REDIS_HOST, REDIS_PORT);
        UnifiedJedis jedis = new UnifiedJedis(hostAndPort, jedisClientConfig);
        jedis.set("foo", "bar");
        jedis.close();
    }

}
